﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    public class Research : Button
    {
        protected int time;
        public string name;
        protected string description;
        protected Timer timer;
        public bool isDeveloped;
        public bool isDeveloping;
        public Research SonRight;
        public Research SonLeft;
        protected bool isDisabled;
        protected User associatedUser;

        private bool hover;

        public static bool cheat;

        public Research(Vector2 _position, Texture2D _texture, User au)
            : base(_position, _texture)
        {
            associatedUser = au;
            cheat = false;
            isDeveloped = false;
            isDeveloping = false;
            isDisabled = false;
            description = "description de la technologie souhaitee par l'utilisteur";
        }

        private void changeSon(bool toLeft, Research researchToAdd)
        {
            if (toLeft)
                SonLeft = researchToAdd;
            else
                SonRight = researchToAdd;
        }

        public virtual void competance()
        {
        }

        public void Develop()
        {
            if (!isDeveloped && !isDeveloping)
                isDeveloping = true;

            if (time == 0)
            {
                isDeveloped = true;
                isDeveloping = false;

                if (SonLeft == null && SonRight == null && !Game.CurrentGame.Online )
                    AchievementManager.AchievementList[16].Add();
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (isDeveloped)
            {

                if (SonLeft != null)
                {

                    if (SonLeft.isDeveloped)
                        spriteBatch.Draw(GameTexture.Link, new Vector2(Position.X - 40, Position.Y + 10), Color.Cyan);
                    else if (!SonLeft.isDisabled)
                        spriteBatch.Draw(GameTexture.Link, new Vector2(Position.X - 40, Position.Y + 10), Color.Blue);
                    else
                        spriteBatch.Draw(GameTexture.Link, new Vector2(Position.X - 40, Position.Y + 10), Color.Red);

                    SonLeft.Draw(spriteBatch);
                }
                if (SonRight != null)
                {


                    if (SonRight.isDeveloped)
                        spriteBatch.Draw(GameTexture.Link, new Rectangle((int)Position.X, (int)Position.Y + 10, GameTexture.Link.Width, GameTexture.Link.Height), null, Color.Cyan, 0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0f);
                    else if (!SonRight.isDisabled)
                        spriteBatch.Draw(GameTexture.Link, new Rectangle((int)Position.X, (int)Position.Y + 10, GameTexture.Link.Width, GameTexture.Link.Height), null, Color.Blue, 0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0f);
                    else
                        spriteBatch.Draw(GameTexture.Link, new Rectangle((int)Position.X, (int)Position.Y + 10, GameTexture.Link.Width, GameTexture.Link.Height), null, Color.Red, 0f, Vector2.Zero, SpriteEffects.FlipHorizontally, 0f);

                    SonRight.Draw(spriteBatch);

                }
                spriteBatch.Draw(Texture, new Vector2(Position.X - Texture.Width / 2, Position.Y - Texture.Height / 2), Color.White);
            }
            else
            {
                spriteBatch.Draw(Texture, new Vector2(Position.X - Texture.Width / 2, Position.Y - Texture.Height / 2), Color.Black);
                if (!isDisabled)
                {
                    string duration = time.ToString();
                    if (time <= 0)
                        duration = GameData.GameString[203];
                    spriteBatch.DrawString(GameData.Font_Simple, duration, new Vector2(Position.X - 20, Position.Y), Color.White);
                }

            }

            if (hover)
                DrawStrings(spriteBatch);
        }

        private void DrawStrings(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(GameData.Font_Simple, name, new Vector2(432, 552), Color.Black);
            spriteBatch.DrawString(GameData.Font_Simple2, description, new Vector2(450, 590), Color.DimGray);
        }

        public void Update(MouseState mouse)
        {
            hover = IsTouched(mouse);
            if (!isDisabled)
            {
                if (isDeveloped)
                    competance();
                if (isDeveloping)
                {
                    time = timer.Duration - timer.ElapsedTime;
                    if (cheat)
                        time = 0;
                    if (time > 0)
                        timer.UpdateTimer();
                }

                if (SonLeft != null)
                {
                    SonLeft.Update(mouse);
                    if ((SonLeft.isDeveloped || SonLeft.isDeveloping) && SonRight != null)
                        SonRight.isDisabled = true;
                }
                if (SonRight != null)
                {
                    SonRight.Update(mouse);
                    if ((SonRight.isDeveloped || SonRight.isDeveloping) && SonLeft != null)
                        SonLeft.isDisabled = true;
                }

                if (IsPressed(mouse))
                    Develop();
            }
        }

        public void Update()//POUR IA ONLY
        {
            if (!isDisabled)
            {  
                if (isDeveloped)
                    competance();
                if (isDeveloping)
                {
                    time = timer.Duration - timer.ElapsedTime;
                    if (time > 0)
                        timer.UpdateTimer();
                }

                if (SonLeft != null)
                {
                    SonLeft.Update();
                    if ((SonLeft.isDeveloped || SonLeft.isDeveloping) && SonRight != null)
                        SonRight.isDisabled = true;
                }
                if (SonRight != null)
                {
                    SonRight.Update();
                    if ((SonRight.isDeveloped || SonRight.isDeveloping) && SonLeft != null)
                        SonLeft.isDisabled = true;
                }

                if (time == 0)
                {
                    isDeveloped = true;
                    isDeveloping = false;
                }
            }
        }

        public void cancel()
        {
            if (!isDisabled)
            {
                if (SonRight != null && SonRight.isDeveloping)
                {
                    SonRight.isDeveloping = false;
                    if (SonLeft != null)
                        SonLeft.isDisabled = false;
                }
                else if (SonLeft != null && SonLeft.isDeveloping)
                {
                    SonLeft.isDeveloping = false;
                    if (SonRight != null)
                        SonRight.isDisabled = false;
                }
                else
                {
                    if (SonRight != null)
                        SonRight.cancel();
                    if (SonLeft != null)
                        SonLeft.cancel();
                }
            }
        }
        /*
        public void changeTime(int _time)
        {
            time = _time;
        }
         * */
    }
}
